<?php

/**
 * Description of VotacionesController
 *
 * @author MFerreira
 * @export Modules
 * @export-metadata RequiredRoles:ROLE_STUDENT:ROLE_PROFESSOR
 * GetDocentes
 */
class Votaciones extends Module {

    private function isHabilitado() {
        $this->assign("cancel", $this->route->moduleAction("FrontPage"));
        $habilitado = $this->model->votes->IsHabilitado();

        return $habilitado;
    }

    private function isInTime() {
        $start = mktime(09, 00, 00, 3, 31, 2011);
        $end = mktime(19, 30, 00, 3, 31, 2011);
        $actual = time();

        return ($start <= $actual && $actual <= $end);
    }

    public function index() {
        $habilitado = $this->isHabilitado();
        $inTime = $this->isInTime();
        if ($habilitado) {
            if ($inTime) {
                $this->showIndex();
            } else {
                $this->addMessage(new Message("El horario de votaci&oacute;n es de 9:00am a 7:30pm", "Las votaciones no est&aacute;n disponibles", Message::SECURE_AREA));
                $this->display("alreadyVoted");
            }
        } else {
            $this->addMessage(new Message("No se puede votar dos veces.", "Usted ya ha votado", Message::SECURE_AREA));
            $this->display("alreadyVoted");
        }
    }

    private function showIndex() {
        $representantes = $this->getRepresentantes();

        if (count($representantes) == 0) {
            $this->addMessage(new Message("No hay representantes.", "", Message::WARNING));
        }

        $servicesURI = ModulesContext::instance()->getConfig()->acawebServicesURI;
        $this->assign("imgURL", $servicesURI . "Resource/Aspirantes/");
        $this->assign("Representantes", $representantes);
        $this->assign("action", $this->route->action("Votar"));
        $this->display("index");
    }

    private function getRepresentantes() {
        $roles = $this->currentUser->getPermissions();
        $rol = $roles[0];
        $representantes = array();
        if ($rol == "ROLE_STUDENT") {
            $estudiante = $this->model->students->GetById($this->currentUser->getName());
            $representantes = $this->model->aspirant->GetByFacultad($estudiante->getPlanEstudio()->getCodigoFacultad());
        } else {
            $representantes = $this->model->aspirant->GetDocentes();
        }
        return $representantes;
    }

    public function Votar() {

        $codigo = $this->request->post("Representante");

        if ($codigo && $this->isHabilitado() && $this->isInTime()) {
            Loader::using("models::entities::Votos");
            $vote = new Votos();
            $vote->setCodigoAspirante($codigo);
            $this->model->votes->HackCreate($vote);

            $this->addMessage(new Message("Su voto ha sido registrado con &eacute;xito.", "Listo", Message::SUCCESS));

            $this->assign("volver", $this->route->moduleAction("FrontPage"));
            $this->display();
        } else {
            $this->index();
        }
    }

}

?>
